1

I'm currently using htaccess in my personal project but i could not make urls works like sitename.com/a/b/c

My base urls: sitename.com/index.php?category=category-name&page=3 How to make this url like sitename.com/category-name/3 or sitename.com/category-name/3/page-name (or page-name.html)

my .htaccess file:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z]+)/([0-9]+)/?$ index.php?category=$1&page=$2 [L]

It does not work and css, js file does not work. How to make this works? Can anyone help me?

zx81
  • 41,100
  • 9
  • 89
  • 105
mstfdrmz
  • 155
  • 6

1 Answers1

0

Try this:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(\w+)/([0-9]+)/?$ index.php?category=$1&page=$2 [L]
RewriteRule ^(\w+)/([0-9]+)/(\w+)/?$ index.php?category=$1&page=$2&page-title=$3 [L]
zx81
  • 41,100
  • 9
  • 89
  • 105
  • The rule above handles `sitename.com/category-name/3`. For `sitename.com/category-name/3/page-name`, please explain where that should redirect. – zx81 Jul 20 '14 at 20:07
  • Categorty: =$_GET['category'];?>
    Page: =$_GET['page'];?> works fine but .css file not found error occurs. And also image files have the same error. I tried on localhost and on the server. The problem the same.
    – mstfdrmz Jul 20 '14 at 20:23
  • for sitename.com/category-name/3/page-name => index.php?category=category-name&page=3&page-title=title i can do if i could realize how it works. Just does not know that why do not found css, js, image files etc. I don't want to use html tag. – mstfdrmz Jul 20 '14 at 20:26
  • For the css that is not found, are you certain that the url is valid? What happens if you type it directly in the browser? Also did you have a rule with a 301 redirect at one stage? If so, try in a different browser as 301s are cached. – zx81 Jul 20 '14 at 20:28
  • Added a rule for the page-title situation and slightly tweaked the other one, give that a try. – zx81 Jul 20 '14 at 20:31
  • css files on http://localhost/Web_Applications/htaccess/css/style.css When on the http://localhost/Web_Applications/htaccess/category/3 page If I include css file like without a / Css file => http://localhost/Web_Applications/htaccess/categorty/3/css/style.css ------------- If I include css file like with a / Css file => http://localhost/css/style.css – mstfdrmz Jul 20 '14 at 20:38
  • Okay, so you want to redirect the css files too? IMO that's not a great use of mod-rewrite—you're consuming resources instead of serving the page directly. The only purpose that serves is to save you specifying the right folder in the PHP code... And that's really something that should be handled in the web page... The rewrites are useful to make urls prettier, but in this case they're not needed... However, if you do want to do that, tell me where the css requests should redirect, and we'll add a rule. – zx81 Jul 20 '14 at 20:54
  • Directories are: /css/ /css/plugins/ /js/ /js/plugins/ /images/ I just want to make prettier urls like sitename.com/foo/bar/etc Instead of using absolute path, relative paths are simple for me. But static files are not found :/ Using tag solves this problem but many of websites do not use this tag. Instead of using this tag they use /css/style.css or /js/jquery.js etc. And web site urls like sitename.com/books/2/bookname. I could not make urls like this. – mstfdrmz Jul 20 '14 at 21:03
  • I have solved the pretty url problem. The css problem is not a rewrite problem. You have to use the right path in your code. I don't think you want to hear that, so I am giving up. – zx81 Jul 20 '14 at 21:09
  • Thanks for your help. I will search on the web if I find what I search I'll post on here, soon (I hope, i could find). Thanks again. – mstfdrmz Jul 20 '14 at 21:12
  • Hey Mustafa, here's a [good question here on Stack about the base tag](http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag) – zx81 Jul 20 '14 at 21:17