php - .htaccess won't rewrite correctly -


i'm having issues .htaccess file, stored @ root level of website.

i want rewrite following

  1. mysite.com executes mysite.com/index.php?lang=en rewrites url mysite.com/en/
  2. mysite.com/en (without trailing slash) same above
  3. mysite.com/en/blog/page executes mysite.com/blog/page?lang=en

in nutshell, add language url, opening index.php either .com, .com/en or .com/en/, , strip file extension

here complete file reference:

rewriteengine on  # redirect domain without www. rewritecond %{https} off rewritecond %{http_host} ^www\.(.*)$ [nc] rewriterule .* http://%1%{request_uri} [r=301,l] # same https: rewritecond %{https} on rewritecond %{http_host} ^www\.(.*)$ [nc] rewriterule .* https://%1%{request_uri} [r=301,l]  # stop hotlinking. rewritecond %{http_referer} !^$ rewritecond %{http_referer} ^https?://([^/]+)/ [nc] rewritecond %1#%{http_host} !^(.+)#\1$ rewriterule \.(jpg|jpeg|png|gif|svg)$ - [nc,f,l]  # prevent directory listings options -indexes  # request language url # empty url -> redirect en/ rewritecond %{query_string} !lang=(en|de) rewriterule ^$ en/ [r=301,l]  # urls have en/ de/ -> parse them rewriterule ^(en|de)/(.*)$  $2?lang=$1&%{query_string} [l]  rewritecond %{request_filename} !-f rewriterule ^([^\.]+)$ $1.php [nc,l] 

for full disclosure, has been taken variety of google results, , appreciate perhaps done better.

as stands, mysite.com gives me

the requested url /home/towrafvk/public_html/en/.php not found on server.

mysite.com/en gives me

the requested url /en.php not found on server.

mysite.com/en/ , mysite.com/en/blog/page both work intended

i know issue in third-to-last , second-to-last rewrite rules. how can fix this?

rewriteengine on  # redirect domain http(s) without www. rewritecond %{http_host}#%{https}s ^www\.([^#]+)#(?:off|on(s)) [nc] rewriterule ^ http%2://%1%{request_uri} [ne,l,r=301]  # stop hotlinking. rewritecond %{http_referer} !^$ rewritecond %{http_referer} ^https?://([^/]+)/ [nc] rewritecond %1#%{http_host} !^(.+)#\1$ rewriterule \.(jpg|jpeg|png|gif|svg)$ - [nc,f,l]  # prevent directory listings options -indexes  # request language url # empty url -> redirect en/ rewritecond %{query_string} !lang=(en|de) rewriterule ^$ en/ [r=301,l]  # urls have en/ de/ -> parse them rewriterule ^(en|de)/(.*)$  $2?lang=$1 [l,qsa]  # use .php file if not exist without , exist .php rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewritecond %{document_root}/$1\.php -f rewriterule ^(.+)/?$ $1.php [l] 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -