DaveyK Posted March 18, 2013 Share Posted March 18, 2013 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 https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/ Share on other sites More sharing options...
requinix Posted March 18, 2013 Share Posted March 18, 2013 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 https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/#findComment-1419390 Share on other sites More sharing options...
DaveyK Posted March 19, 2013 Author Share Posted March 19, 2013 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 https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/#findComment-1419482 Share on other sites More sharing options...
requinix Posted March 19, 2013 Share Posted March 19, 2013 Stupid slashes. Try matching against ^/?blog/([^/]+)/(\d+)$. Link to comment https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/#findComment-1419485 Share on other sites More sharing options...
DaveyK Posted March 19, 2013 Author Share Posted March 19, 2013 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 https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/#findComment-1419490 Share on other sites More sharing options...
DaveyK Posted March 19, 2013 Author Share Posted March 19, 2013 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 https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/#findComment-1419523 Share on other sites More sharing options...
requinix Posted March 19, 2013 Share Posted March 19, 2013 That period I had in my post? Stupid slashes. Try matching against ^/?blog/([^/]+)/(\d+)$.That was an "end of sentence" period, not a "metacharacter that goes in the regex" period RewriteRule ^/?blog/([^/]+)/(\d+)$ blog.php?title=$1&id=$2 [L] Link to comment https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/#findComment-1419636 Share on other sites More sharing options...
DaveyK Posted March 19, 2013 Author Share Posted March 19, 2013 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 Link to comment https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/#findComment-1419640 Share on other sites More sharing options...
requinix Posted March 19, 2013 Share Posted March 19, 2013 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 Link to comment https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/#findComment-1419654 Share on other sites More sharing options...
DaveyK Posted March 19, 2013 Author Share Posted March 19, 2013 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 Link to comment https://forums.phpfreaks.com/topic/275810-htaccess-vanity-url/#findComment-1419658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.