Mobile app version of vmapp.org
Login or Join
Ogunnowo487

: Rewrite url /powers/power.php?power I want to rewrite my url, the url uses a variable I think its called, but I want to shorten the url to http://xatwebs.co/powers/gold as it is currently http://xatwebs.co/powers/power?power=

@Ogunnowo487

Posted in: #Htaccess #Redirects

I want to rewrite my url, the url uses a variable I think its called, but I want to shorten the url to xatwebs.co/powers/gold as it is currently xatwebs.co/powers/power?power=gold I have search for rewrite methods but the ones i have tried has either broke it completely or made a loop redirect making the page non viewable

Redirect 301 /trusted community.xat.com/showthread.php?28330-Prize-Holders Redirect 301 /forums xatwebs.co/forum RewriteEngine on
RewriteBase /

# force url to lowercase
RewriteCond %{REQUEST_URI} [A-Z]
# ensure it is not a file on the drive first
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule (.*) rewrite-strtolower.php?rewrite-strtolower-url= [QSA,L]
RewriteEngine on
#
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule ^(([^/]+/)*[^.]+)$ /.php [L]

ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
RewriteCond %{HTTP_HOST} ^xatwebs.co$ [OR]
RewriteCond %{HTTP_HOST} ^www.xatwebs.co$
RewriteRule ^notice1$ "http://assistance.xatwebs.co/general/inactivity-notice/" [R=302,L]

RewriteRule ^powers/(.+)$ /power.php?power= [QSA,NC,L]


Update: I think I have fixed it by using this code as it allows me to do xatwebs.co/powers/red but it doesn't redirect from xatwebs.co/powers/power.php?power=red

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.xatwebs.co
RewriteRule (.*) xatwebs.co/powers/ [R=301,L]

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ power.php?power=
RewriteRule ^([a-zA-Z0-9_-]+)/$ power.php?power=

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Ogunnowo487

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

You only need RewriteEngine On once at the top of your .htaccess file.

Always have external redirects before internal rewrites.

So, in order to redirect from the "ugly" (/powers/power.php?power=red) URL to the "pretty" (/powers/red) URL (in order to satisfy search engines and anyone who has linked to your site), you could do something like this:

# Redirect typed querystring URLs to "pretty" URLs
RewriteCond %{THE_REQUEST} ?power=([^ ]*)
RewriteRule ^powers/power.php$ /powers/%1? [R=301,L]


This checks against THE_REQUEST to avoid a potential rewrite loop (caused by the following rewrite).

Then to internally rewrite from the "pretty" URL back to the "ugly" (real) one, something similar to what you have after your update:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^power=
RewriteRule ^powers/(.*) /powers/power.php?power= [L]


Although, from your examples, it's a bit unclear whether /powers is part of the URL or not (you mention it in the description but it appears to be omitted from your "working" update)? So this may need adjusting. This also replaces your extensionless URLs code snippet as well.

Don't rewrite direct requests for files that already exist on the filesystem - just in case you have scripts or stylesheets that match this pattern. Also this doesn't rewrite requests that already have the query string parameter.

Make (.*) more restrictive if you need to.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme