Jump to content

.htaccess vanity URL


DaveyK
Go to solution Solved by requinix,

Recommended Posts

Hey guys,

 

I'm in the process of creating a website and Im currently adding a blog. I have the htaccess working so far, but I dont know how to add another condition for the blog and htaccess is really tricky for me...

 

regardless, this is current htaccess:

 

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
</IfModule>

# If requested resource exists as a file or directory, skip next two rules
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [s=2]

# Remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?page=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app.php?tag=$1 [QSA,L]

# ----------------------------------------------------------------------
# Custom 404 page
# ----------------------------------------------------------------------

# You can add custom pages to handle 500 or 403 pretty easily, if you like.
ErrorDocument 403 /error.php?type=403
ErrorDocument 404 /error.php?type=404
ErrorDocument 500 /error.php?type=500

This works, though I have no idea if its in any way correct. What I need is a normal vanity url thing which would rewrite

 

/blog/some-blog-title-here/1

 

to

 

/blog.php?title=some-blog-title&id=1

 

but obviously the user would see the first option. Any help or suggestions?

Link to comment
Share on other sites

Follow the typical REQUEST_FILENAME !-f and !-d pattern, then match against the URL structure.

 

Also, I can't help but clean up a little what you have there.

Options +FollowSymlinks

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Remove the .php extension
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule ^([^.]+)$ $1.php [NC,L]

  # Blog articles
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^blog/([^/]+)/(\d+)$ blog.php?title=$1&id=$2 [L]

  #RewriteCond %{REQUEST_FILENAME} !-f
  #RewriteCond %{REQUEST_FILENAME} !-d
  #RewriteRule ^(.*)$ index.php?page=$1 [L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ app.php?tag=$1 [QSA,L]
</IfModule>

# ----------------------------------------------------------------------
# Custom 404 page
# ----------------------------------------------------------------------

# You can add custom pages to handle 500 or 403 pretty easily, if you like.
ErrorDocument 403 /error.php?type=403
ErrorDocument 404 /error.php?type=404
ErrorDocument 500 /error.php?type=500
Link to comment
Share on other sites

hey requinix,

 

thanks for the effort (and of course for cleaning it up), and I used what you posted, but somehow I am still getting redirected to the wrong page...

I am using this url:

 

http://localhost/blog/title/1

 

but when I run the $_SERVER variable it turns out I'm on app.php

 

[sCRIPT_FILENAME] => C:/Users/(...)/app.php

For reference, the htaccess I am using is:

 

Options +FollowSymlinks
 
<IfModule mod_rewrite.c>
RewriteEngine On
 
# Remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [NC,L]
 
# Blog articles
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([^/]+)/(\d+)$ blog.php?title=$1&id=$2 [L]
 
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?page=$1 [L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app.php?tag=$1 [QSA,L]
</IfModule>
 
# ----------------------------------------------------------------------
# Custom 404 page
# ----------------------------------------------------------------------
 
# You can add custom pages to handle 500 or 403 pretty easily, if you like.
ErrorDocument 403 /error.php?type=403
ErrorDocument 404 /error.php?type=404
ErrorDocument 500 /error.php?type=500

Am I doing something wrong?

Link to comment
Share on other sites

Well, again thanks for the suggestion, but I tried what you mentioned

 

^/?blog/([^/]+)/(\d+)$.

 

(both with and without that last dot, didnt know if that was an error or not). Leading to this .htaccess file:

 

Options +FollowSymlinks
 
<IfModule mod_rewrite.c>
RewriteEngine On
 
# Remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [NC,L]
 
# Blog articles
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^/?blog/([^/]+)/(\d+)$ blog.php?title=$1&id=$2 [L]
 
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?page=$1 [L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app.php?tag=$1 [QSA,L]
</IfModule>
 
# ----------------------------------------------------------------------
# Custom 404 page
# ----------------------------------------------------------------------
 
# You can add custom pages to handle 500 or 403 pretty easily, if you like.
ErrorDocument 403 /error.php?type=403
ErrorDocument 404 /error.php?type=404
ErrorDocument 500 /error.php?type=500

this is the version where I left the dot (.) out. Sadly, $_SERVER says:

 

[REQUEST_URI] => /blog/title/1
[sCRIPT_NAME] => /app.php
[php_SELF] => /app.php 

Im sorry for being such a pain in the.... but I dont know whats wrong :(

Link to comment
Share on other sites

I just realized the .htaccess displayed in the previous post was not right. heres the right one

 

Options +FollowSymlinks
 
<IfModule mod_rewrite.c>
RewriteEngine On
 
# Remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [NC,L]
 
# Blog articles
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?blog/([^/]+)/(\d+)$. blog.php?title=$1&id=$2 [L]
 
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?page=$1 [L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app.php?tag=$1 [QSA,L]
</IfModule>
 
# ----------------------------------------------------------------------
# Custom 404 page
# ----------------------------------------------------------------------
 
# You can add custom pages to handle 500 or 403 pretty easily, if you like.
ErrorDocument 403 /error.php?type=403
ErrorDocument 404 /error.php?type=404
ErrorDocument 500 /error.php?type=500

the result is the same though

Link to comment
Share on other sites

Nope, no success. One thing I would like to point out is that, in your clean up, something was removed and I suddenly had 404's everywhere.

So I restored to the old version and added the rule you suggested, leading to this .htaccess:

 

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
 
# If requested resource exists as a file or directory, skip next two rules
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [s=2]

# Remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# Blog articles
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?blog/([^/]+)/(\d+)$ blog.php?title=$1&id=$2 [L]

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?page=$1 [L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app.php?tag=$1 [QSA,L]
 
# ----------------------------------------------------------------------
# Custom 404 page
# ----------------------------------------------------------------------
 
# You can add custom pages to handle 500 or 403 pretty easily, if you like.
ErrorDocument 403 /error.php?type=403
ErrorDocument 404 /error.php?type=404
ErrorDocument 500 /error.php?type=500

Sadly, this still doesnt work. I hate this language, whatever it is.I can never get it to work properly.

 

EDIT: grammar

Edited by DaveyK
Link to comment
Share on other sites

  • Solution

In my rewrite I positioned in the blog thing to be before the $1.php rule you now have immediately before. Because the two conflict: Apache will try the REQUEST_FILENAME as "/blog/title/1", add an extension because that file doesn't exist, continue with "blog/title/1.php", and that doesn't match the actual /blog rule.

So move them around. Rule of thumb for URL rewriting: most specific first, least specific last.

1. Skip first (which needs to skip three rules now)

2. Most specific: /blog

3. Less specific: try adding the .php extension

4. Least specific: route everything else through index.php

 

[edit] I mean through index.php, not app.php

Edited by requinix
Link to comment
Share on other sites

You rock. What you pointed out makes a lot of sense, and so I changed the .htaccess file:

 

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
 
# If requested resource exists as a file or directory, skip next two rules
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [s=3]

# Blog articles
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?blog/([^/]+)/(\d+)$ blog.php?title=$1&id=$2 [L]

# Remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?page=$1 [L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app.php?tag=$1 [QSA,L]
 
# ----------------------------------------------------------------------
# Custom 404 page
# ----------------------------------------------------------------------
 
# You can add custom pages to handle 500 or 403 pretty easily, if you like.
ErrorDocument 403 /error.php?type=403
ErrorDocument 404 /error.php?type=404
ErrorDocument 500 /error.php?type=500

And this works. Thank you so much.

 

EDIT: PS, if you have any suggestions as to how this can be tidied up, I would still be more than willing to implement them. As long as it works :)

Edited by DaveyK
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.