Given urls like this:
mysite.com/index.php
mysite.com/page/member/lobby.php
mysite.com/page/videos/video1.php
How can I rewrite the urls with .htaccess to hide the /page/ folder when it's present?
So the end result is:
mysite.com/index.php
mysite.com/member/lobby.php
mysite.com/videos/video1.php
You can use this rule to add page/ to your path internally:
RewriteCond $1 !=page
RewriteRule ^([^/]+)/.+ page/$0 [L]
Now every request that’s URI path has at least two path segments but that’s first segment is not page will be prefixes with /page. So /member/lobby.php will be rewritten to /page/member/lobby.php.
Related
i want convert all external link to internal and redirect them
let me show you what i want:
i have an external link to my website like this:
link
and i converted it to:
link
OR
link
OR somthing like this
how can i use .htaccess to make sure when someone opened the link he goes to:
https://external.com/some text here
i tried this but id didn't worked
RewriteEngine On
RewriteRule ^mywebsite.com/redirect/(.*)$ /$1 [NC,L]
RewriteRule ^mywebsite.com/redirect/(.*)$ /$1 [NC,L]
RewriteRule only matches against the path component of the URL. (And when configured in .htaccess context, the path it checks against never starts with a leading slash, the path to the current directory has been stripped off at this point already.)
/$1 would cause a slash before the actual substitution URL.
Both fixed, it should simply look like this:
RewriteRule ^redirect/(.*)$ $1 [L]
I have the following folders structure on my web server:
I need some rewrite rules:
When I visit http://testdomain1.com/ I need to set /pages/testdomain1.com/ as the root directory to display content from this dir.
Similarly, http://testdomain2.com/ root folder is /pages/testdomain2.com/ etc..
When the requested URL has the la var - I need to rewrite to landing directory.
Example:
http://testdomain1.com/?la=template1 - set /lands/template1/ as root dir
I have some issue with my current .htaccess (for first task):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?testdomain1.com$
RewriteRule ^(/)?$ pages/%{HTTP_HOST}/index.html [L]
Rewrite rule works, but all assets data (js, css) don't work (404 not found).
How can I set the right rules?
... but all assets data (js,css) don't work (404 not found). How can I set right rules?
This is most probably caused by using relative URL paths to your assets. You need to "fix" your URL paths to be root-relative or absolute. This is not something that should be fixed in .htaccess, your rewrite is otherwise OK.
See my answer to the following question on the Webmasters stack, relating to the use of relative URL paths to static assets when using URL rewriting:
https://webmasters.stackexchange.com/questions/86450/htaccess-rewrite-url-leads-to-missing-css
RewriteCond %{HTTP_HOST} ^(www.)?testdomain1.com$
RewriteRule ^(/)?$ pages/%{HTTP_HOST}/index.html [L]
This only rewrites requests for the root, ie. testdomain1.com/. And not testdomain1.com/foo - is that intentional?
I need to have access to any page (example.com/page.html) not only index.html.
If by that you mean that a request of the form http://testdomain1.com/foo should be rewritten to /pages/testdomain1.com/foo, then you would need to change the above to something like the following:
RewriteCond %{QUERY_STRING} !^la=[\w-]+
RewriteCond %{HTTP_HOST} ^(?:www.)?(testdomain1\.com)$
RewriteRule !^pages/ pages/%1%{REQUEST_URI} [L]
Note that this rewrites everything, including CSS, JS and images.
The first condition (RewriteCond directive) excludes URLs that contain the la URL parameter. (These URLs are handled below.)
This also fixes a bug in your existing directive, whereby a request for the www subdomain (eg. www.testdomain1.com) would have resulted in an incorrect rewrite. The %1 backreference in the substitution string contains the domain being requested - less an optional www subdomain. (I assume you don't have a subdirectory www.testdomain1.com?) Although, arguably, you should already be canonicalising the www/non-www earlier in the config file.
(Remember to escape literal dots.)
UPDATE:
i tried to use this case for multiple domains, not only for testdomain1.com. So i need change RewriteCond %{HTTP_HOST} ^(www.)?testdomain1.com$ part. I just tried RewriteCond %{REQUEST_URI} but don't working.
For multiple domains, you can either:
Duplicate the above code block for each domain and change the testdomain1.com domain in the second condition.
If this applies to just 3 domains then you can use alternation in the second condition, to match any of the 3 domains. For example:
RewriteCond %{HTTP_HOST} ^(?:www.)?(testdomain1\.com|testdomain2\.com|testdomain3\.com)$
If this should apply to any domain, then make the regex more general to match any domain. For example:
RewriteCond %{HTTP_HOST} ^(?:www.)?([a-z0-9.-]+?)\.?$
2) When request URL have "la" var - I need rewrite to land directory.
Example: http://testdomain1.com/?la=template1 - set /lands/template1/ as root dir
As mentioned in my comment, you probably shouldn't be rewriting to a directory. It is not the directory that handles the request, but ultimately a file. By rewriting to a directory I assume you are relying on mod_dir issuing an internal subrequest for the directory index document (as specified by the DirectoryIndex directive). For the sake of this example, I will assume the directory index is index.php.
This would need to go before the above rewrite, since it is more specific:
RewriteCond %{QUERY_STRING} ^la=([\w-]+)
RewriteRule ^$ /lands/%1/index.php [L]
%1 (as opposed to $1) is a backreference to the captured pattern in the last matched CondPattern. The regex [\w-]+ restricts the la parameter value to the characters 0-9, a-z, A-Z, _ and -. The la parameter must also be the first URL parameter.
I have duplicate urls and i need to do some redirection in htaccess
Examples:
Original Urls are :
www.mywebsite.com/listofgames/pacman.html
www.mywebsite.com/listofgames/nintendo.html
www.mywebsite.com/listofgames/snake.html
I have about 1000 games name
Example of duplicate urls :
www.mywebsite.com/listofgames/1/pacman.html
www.mywebsite.com/listofgames/1521/pacman.html
www.mywebsite.com/listofgames/52154/pacman.html
www.mywebsite.com/listofgames/abc/pacman.html
www.mywebsite.com/listofgames/opq/pacman.html
www.mywebsite.com/listofgames/opq/1/2/35/pacman.html
www.mywebsite.com/listofgames/154/3562/snake.html
www.mywebsite.com/listofgames/2541/snake.html
www.mywebsite.com/listofgames/524/snake.html
www.mywebsite.com/listofgames/absc/gtar/15/nintendo.html
www.mywebsite.com/listofgames/nije/nintendo.html
I need to redirect them to original urls
So
www.mywebsite.com/listofgames/anynumber/mygamesname.html
www.mywebsite.com/listofgames/anyword/mygamesname.html
www.mywebsite.com/listofgames/anyword/anynumber/anyword/mygamesname.html
Must be redirected to
www.mywebsite.com/listofgames/mygamesname.html
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/listofgames/.*/([^\.]+)\.html/? [NC]
RewriteRule .* listofgames/%1.html [R=301,L]
Redirects permanently
http://www.mywebsite.com/listofgames/any/number/of/folders/mygamesname.html
with or without trailing slash.
To:
http://www.mywebsite.com/listofgames/mygamesname.html
Effectively removing the segment path between /listofgames/ folder and the HTML file.
String listofgames and extension html are assumed to be fixed, while mygamesname is assumed to be variable.
The incoming URL structure has to be kept for the rule-set to work: Last string inside the URL must be the HTML file.
For silent mapping, remove R=301 from [R=301,L]
I'm trying to change this URL:
domain.com/?ACT=52&id=28
to this one:
domain.com/site.html
I want to replace the last segment ?ACT=52&id=28 to site.html. There are no parameters to consider. The URL domain.com/?ACT=52&id=28 will always be the same.
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ACT=52&id=28$
RewriteRule ^/?$ /site.html [L]
This makes it so when someone goes to domain.com/?ACT=52&id=28, they get served the content at domain.com/site.html, but if you to change the URL that's in the browser's URL address bar, you need to REDIRECT, not internally rewrite, so add the redirect flag in the square brackets: [L,R]
I have urls like so:
http://www.domain.com/account/home
http://www.domain.com/account/settings/email
http://www.domain.com/account/widget
http://www.domain.com/account/widget/2
http://www.domain.com/account/...
I would like to rewrite the url only when widget is in the 2nd segment and for it to be rewritten without the 1st segment account like so:
http://www.domain.com/widget
http://www.domain.com/widget/2
http://www.domain.com/widget/...
How would I go about doing this? I know how to remove account entirely but not selectively.
NB account is a subfolder that my website resides in.
[EDIT]
I have found this to work, is this the best way to do it?
RewriteEngine on
RewriteRule ^account/widget/(.*)$ /widget/$1 [L]
RewriteEngine on
RewriteRule ^account/widget/(.*)$ /widget/$1 [L]